<html>
<head>
<script>

function log(message)
{
    document.body.innerHTML += message + "<br>";
}

function terminateTest()
{
    if (window.testRunner)
        testRunner.notifyDone();
}

function openTestDatabase()
{
    return openDatabase("ReadTransactionsRunningConcurrentlyTest",
                        "1.0",
                        "Test to make sure that multiple read transactions on different DB handles to the same DB run concurrently.",
                        32768);
}

var readTransactionsInProgress = 0;
var done = false;

function runReadTransaction(db)
{

    db.readTransaction(
        function(tx) {
            ++readTransactionsInProgress;
            if (readTransactionsInProgress === 2) {
                log("Read transactions running concurrently.");
                done = true;
            }

            (function spin() {
                if (done)
                    return;
                tx.executeSql("SELECT ? AS zero", [0], spin,
                    errorHandler("execute failed"));
            }());
        },
        errorHandler("Read transaction failed"),
        function() {
            readTransactionsInProgress--;
            if (readTransactionsInProgress === 0)
                terminateTest();
        });
}

function errorHandler(message) {
    return function(error) {
        log(message + ": " + error.message);
        terminateTest();
    };
}

function runTest() {
    if (window.testRunner) {
        testRunner.clearAllDatabases();
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
    }

    try {
        var db1 = openTestDatabase();
        var db2 = openTestDatabase();
        db1.transaction(
            function(tx) {
                tx.executeSql("CREATE TABLE IF NOT EXISTS Test (Foo int);");
            },
            errorHandler("Cannot create the Test table"),
            function() {
                runReadTransaction(db1);
                runReadTransaction(db2);
            });
     } catch(err) { log(err); }
}
</script>
</head>
<body onload="runTest();">
This test tests that two read-only transactions on different handles to the same database run concurrently.<br>
</body>
</html>
